package cn.lbd.datastructure;

public class LinkedList {

    public static void main(String[] args) {
        /*HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(6, "林冲", "豹子头");

        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.add(heroNode4);

        *//*singleLinkedList.list();*//*

        HeroNode heroNode5 = new HeroNode(4, "公孙胜", "入云龙");

        singleLinkedList.addByOrder(heroNode5);
        *//*singleLinkedList.list();*//*

        HeroNode heroNode6 = new HeroNode(4, "公孙胜11111", "入云龙1111");

        singleLinkedList.update(heroNode6);
        *//*singleLinkedList.list();*//*

        singleLinkedList.del(6);
        singleLinkedList.del(3);
        singleLinkedList.del(3);
        singleLinkedList.list();*/


        HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode5 = new HeroNode2(4, "公孙胜", "入云龙");


        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();

        doubleLinkedList.add(heroNode1);
        doubleLinkedList.add(heroNode2);
        doubleLinkedList.add(heroNode3);
        doubleLinkedList.add(heroNode5);

        doubleLinkedList.list();
        System.out.println();

        HeroNode2 heroNode4 = new HeroNode2(6, "林冲", "豹子头");
        doubleLinkedList.addByOrder(heroNode4);
        doubleLinkedList.list();
        System.out.println();


        HeroNode2 heroNode6 = new HeroNode2(5, "关胜", "大刀");
        doubleLinkedList.addByOrder(heroNode6);
        doubleLinkedList.list();
        /*doubleLinkedList.del(6);
        doubleLinkedList.list();*/

       /* HeroNode heroNode5 = new HeroNode(4, "公孙胜", "入云龙");

        doubleLinkedList.addByOrder(heroNode5);
        *//*singleLinkedList.list();*//*

        HeroNode heroNode6 = new HeroNode(4, "公孙胜11111", "入云龙1111");

        doubleLinkedList.update(heroNode6);
        *//*singleLinkedList.list();*//*

        doubleLinkedList.del(6);
        doubleLinkedList.del(3);
        doubleLinkedList.del(3);
        doubleLinkedList.list();*/

    }


}

//节点
class HeroNode2 {
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

class DoubleLinkedList {

    //头节点
    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead(){
        return head;
    }

    //添加节点到单向链表尾部
    public void add(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        while (true) {
            //找到链表的最后一个节点
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    //根据排名将英雄按序插入
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        boolean flag = false; //是否有重复编号
        while (true) {
            //遍历到尾部
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                //找到待插入位置
                break;
            } else if (temp.next.no == heroNode.no) {
                //编号重复
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //插入节点
        if (flag) {
            //有重复编号
            System.out.println("插入编号" + heroNode.no + "已存在");
        } else {
            if (temp.next != null){
                heroNode.pre = temp;
                heroNode.next = temp.next;
                temp.next.pre = heroNode;
                temp.next = heroNode;
            }else {
                temp.next = heroNode;
                heroNode.pre = temp;
            }
        }
    }

    //修改节点的信息，根据编号no来修改，即No编号不能改
    public void update(HeroNode2 heroNode) {
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;//标记是否找到元素

        while(true){
            if (temp.next == null){
                break;
            }

            if (temp.no == heroNode.no){
                flag = true;
                break;
            }
            temp = temp.next;

        }

        if (flag){
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        }else {
            System.out.println("未找到"+heroNode.no+"号英雄");
        }
    }


    //删除节点
    public void del(int no) {
        //遍历到链表尾部
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }

        HeroNode2 temp = head.next;
        boolean flag = false;

        while (true){
            if (temp  == null){
                break;
            }
            if (temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag){
            temp.pre.next = temp.next;
            if (temp.next != null){
                temp.next.pre = temp.pre;
            }
        }else {
            System.out.println("未找到"+no+"号英雄");
        }
    }

    //显示链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        HeroNode2 temp = head.next;

        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }

    }


}

//节点
class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

class SingleLinkedList {

    //头节点
    private HeroNode head = new HeroNode(0, "", "");

    public HeroNode getHead(){
        return head;
    }

    //添加节点到单向链表尾部
    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            //找到链表的最后一个节点
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //根据排名将英雄按序插入
    public void addByOrder(HeroNode heroNode) {
        HeroNode temp = head;
        boolean flag = false; //是否有重复编号
        while (true) {
            //遍历到尾部
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                //找到待插入位置
                break;
            } else if (temp.next.no == heroNode.no) {
                //编号重复
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //插入节点
        if (flag) {
            //有重复编号
            System.out.println("插入编号" + heroNode.no + "已存在");
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    //修改节点的信息，根据编号no来修改，即No编号不能改
    public void update(HeroNode heroNode) {
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        boolean flag = false;//标记是否找到元素

        while(true){
            if (temp.next == null){
                break;
            }

            if (temp.no == heroNode.no){
                flag = true;
                break;
            }
            temp = temp.next;

        }

        if (flag){
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        }else {
            System.out.println("未找到"+heroNode.no+"号英雄");
        }
    }


    //删除节点
    public void del(int no) {
        //遍历到链表尾部
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }

        HeroNode temp = head;
        boolean flag = false;

        while (true){
            if (temp.next == null){
                break;
            }
            if (temp.next.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag){
            temp.next = temp.next.next;
        }else {
            System.out.println("未找到"+no+"号英雄");
        }
    }

    //显示链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        HeroNode temp = head.next;

        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }


}


